parameters:
  jobTemplate: ''
  hostedOs: ''
  hostedArch: ''
  osGroup: ''
  osSubgroup: ''
  archType: ''
  container: ''
  helixQueuesTemplate: ''
  platform: ''
  targetRid: ''
  jobParameters: {}
  shouldContinueOnError: false # set `shouldContinueOnError: forceFalse` to not use the defaults, and set specifically to `false`
  variables: []

jobs:
- template: ${{ coalesce(parameters.helixQueuesTemplate, parameters.jobTemplate) }}
  parameters:
    shouldContinueOnError: ${{ or(eq(parameters.shouldContinueOnError, true), and(ne(parameters.shouldContinueOnError, 'forceFalse'), endsWith(variables['Build.DefinitionName'], 'staging'), eq(variables['Build.Reason'], 'PullRequest'))) }}

    variables:
      - template: /eng/common/templates/variables/pool-providers.yml
      # Disable component governance in our CI builds. These builds are not shipping nor
      # are they a service. Also the component governance jobs issue lots of inconsequential
      # warnings and errors into our build timelines that make it hard to track down
      # real errors in the build
      - name: skipComponentGovernanceDetection
        value: true
      - name: runCodesignValidationInjection
        value: false

      - name: checkoutFetchDepth
        value: 20

      - name: buildConfigUpper
        ${{ if eq(parameters.jobParameters.buildConfig, 'debug') }}:
          value: 'Debug'
        ${{ if eq(parameters.jobParameters.buildConfig, 'release') }}:
          value: 'Release'
        ${{ if eq(parameters.jobParameters.buildConfig, 'checked') }}:
          value: 'Checked'

      - name: _BuildConfig
        value: $(buildConfigUpper)

      - name: archType
        value: ${{ parameters.archType }}

      - name: osGroup
        value: ${{ parameters.osGroup }}

      - name: osSubgroup
        value: ${{ parameters.osSubgroup }}

      - name: _runSmokeTestsOnlyArg
        value: '/p:RunSmokeTestsOnly=$(isRunSmokeTestsOnly)'

      - name: _overrideTestScriptWindowsCmdParameter
        ${{ if eq(parameters.jobParameters.runScriptWindowsCmd, true) }}:
          value: '/p:RunScriptWindowsCmd=true'
        ${{ if ne(parameters.jobParameters.runScriptWindowsCmd, true) }}:
          value: ''

      - name: _hostedOs
        value: ${{ parameters.jobParameters.hostedOs }}

      - ${{ if or(eq(parameters.osGroup, 'windows'), eq(parameters.jobParameters.hostedOs, 'windows')) }}:
        - name: archiveExtension
          value: '.zip'
        - name: archiveType
          value: zip
        - name: tarCompression
          value: ''
        - name: scriptExt
          value: '.cmd'
        - name: dir
          value: '\'
        - name: _msbuildCommand
          value: powershell -ExecutionPolicy ByPass -NoProfile eng\common\msbuild.ps1 -ci
        - name: _warnAsErrorParamHelixOverride
          value: -warnaserror 0
        - name: setScriptToEchoAndFailOnNonZero
          value: ''
        - name: logRootNameArg
          value: 'log '

      - ${{ if and(ne(parameters.osGroup, 'windows'), ne(parameters.jobParameters.hostedOs, 'windows')) }}:
        - name: archiveExtension
          value: '.tar.gz'
        - name: archiveType
          value: tar
        - name: tarCompression
          value: gz
        - name: scriptExt
          value: '.sh'
        - name: dir
          value: '/'
        - name: _msbuildCommand
          value: ./eng/common/msbuild.sh --ci
        - name: _warnAsErrorParamHelixOverride
          value: --warnaserror false
        # Set the bash script to display each command, and stop if any command exits nonzero.
        - name: setScriptToEchoAndFailOnNonZero
          value: 'set -xe'
        - name: logRootNameArg
          value: '-log:'

      - name: runtimeFlavorName
        ${{ if eq(parameters.jobParameters.runtimeFlavor, 'mono') }}:
          value: Mono
        ${{ if eq(parameters.jobParameters.runtimeFlavor, 'coreclr') }}:
          value: CoreCLR

      - ${{ if eq(parameters.archType, 'wasm') }}:
        - name: wasmDarcDependenciesChanged
          value: $[ or(
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.Microsoft_NET_Workload_Emscripten_Current_Manifest-9_0_100_Transport'], true),
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.Microsoft_DotNet_Build_Tasks_Workloads'], true),
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.System_Runtime_TimeZoneData'], true),
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.Microsoft_Net_Compilers_Toolset'], true),
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.Microsoft_CodeAnalysis'], true),
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.Microsoft_CodeAnalysis_CSharp'], true),
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.Microsoft_CodeAnalysis_Analyzers'], true),
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.Microsoft_CodeAnalysis_NetAnalyzers'], true),
                      eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['DarcDependenciesChanged.Microsoft_NET_ILLink_Tasks'], true)) ]

        - name: shouldRunWasmBuildTestsOnDefaultPipeline
          value: $[
            or(
              eq(variables['wasmDarcDependenciesChanged'], true),
              eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['SetPathVars_tools_illink.containsChange'], true),
              eq(stageDependencies.EvaluatePaths.evaluate_paths.outputs['SetPathVars_wasmbuildtests.containsChange'], true))
            ]

        # needed for Wasm.Build.Tests
        - name: wasmSingleThreadedBuildOnlyNeededOnDefaultPipeline
          value: $[ variables['shouldRunWasmBuildTestsOnDefaultPipeline'] ]
        - name: wasmMultiThreadedBuildOnlyNeededOnDefaultPipeline
          value: $[ variables['shouldRunWasmBuildTestsOnDefaultPipeline'] ]

      - ${{ each variable in parameters.variables }}:
        - ${{ variable }}

    osGroup: ${{ parameters.osGroup }}
    osSubgroup: ${{ parameters.osSubgroup }}
    archType: ${{ parameters.archType }}
    targetRid: ${{ parameters.targetRid }}
    platform: ${{ parameters.platform }}

    ${{ if ne(parameters.container, '') }}:
      ${{ if eq(parameters.container.registry, 'mcr') }}:
        container: ${{ format('{0}:{1}', 'mcr.microsoft.com/dotnet-buildtools/prereqs', parameters.container.image) }}
      ${{ if and(ne(parameters.container.image, ''), ne(parameters.container.registry, 'mcr')) }}:
        container: ${{ format('{0}:{1}', parameters.container.registry, parameters.container.image) }}
      ${{ if eq(parameters.container.image, '') }}:
        container: ${{ parameters.container }}

    ${{ if eq(parameters.jobParameters.pool, '') }}:
      pool:
        # Public Linux Build Pool
        ${{ if and(or(in(parameters.osGroup, 'linux', 'freebsd', 'android', 'tizen'), eq(parameters.jobParameters.hostedOs, 'linux')), eq(variables['System.TeamProject'], 'public')) }}:
          name:  $(DncEngPublicBuildPool)
          demands: ImageOverride -equals Build.Ubuntu.2204.Amd64.Open

        # Official Build Linux Pool
        ${{ if and(or(in(parameters.osGroup, 'linux', 'freebsd', 'android', 'tizen'), eq(parameters.jobParameters.hostedOs, 'linux')), ne(variables['System.TeamProject'], 'public')) }}:
          name: $(DncEngInternalBuildPool)
          demands: ImageOverride -equals Build.Ubuntu.2204.Amd64

        # OSX Build Pool (we don't have on-prem OSX BuildPool).
        ${{ if in(parameters.osGroup, 'osx', 'maccatalyst', 'ios', 'iossimulator', 'tvos', 'tvossimulator') }}:
          vmImage: 'macos-12'

        # Official Build Windows Pool
        ${{ if and(or(eq(parameters.osGroup, 'windows'), eq(parameters.jobParameters.hostedOs, 'windows')), ne(variables['System.TeamProject'], 'public')) }}:
          name: $(DncEngInternalBuildPool)
          demands: ImageOverride -equals windows.vs2022preview.amd64

        # Public Windows Build Pool
        ${{ if and(or(eq(parameters.osGroup, 'windows'), eq(parameters.jobParameters.hostedOs, 'windows')), eq(variables['System.TeamProject'], 'public')) }}:
          name: $(DncEngPublicBuildPool)
          demands: ImageOverride -equals windows.vs2022preview.amd64.open


    ${{ if eq(parameters.helixQueuesTemplate, '') }}:
      # macOS hosted pool machines are slower so we need to give a greater timeout than the 60 mins default.
      ${{ if and(eq(parameters.jobParameters.timeoutInMinutes, ''), in(parameters.osGroup, 'osx', 'maccatalyst', 'ios', 'tvos')) }}:
        timeoutInMinutes: 120
      ${{ insert }}: ${{ parameters.jobParameters }}
    ${{ if ne(parameters.helixQueuesTemplate, '') }}:
      jobTemplate: ${{ parameters.jobTemplate }}
      jobParameters: ${{ parameters.jobParameters }}
